package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.PublisherImpl;
import com.shisfish.library.Model.Publisher;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("publisher")
public class PublisherController extends GlobalVariable {

	private PublisherImpl publisherImpl = new PublisherImpl();
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月7日 下午6:48:05
	 * @Descrition : 获得所有的出版社信息
	 * @return
	 */
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Publisher> publishers = publisherImpl.selectAll();
		
		if (publishers.size() > 0) {
			globalMap.put("list", publishers);
			globalMap.put(STATUS_CODE, SUCCESS);
		}
		else if (publishers.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_ERROR);
		}
		
		return toJsonUtils.map2Json(globalMap);
		
	}
	
	@GET
	@Path("selectAllPage/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		
			
		int count = publisherImpl.countAll();
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Publisher> publishers = publisherImpl.selectAllPage(page.getBeginIndex(), pageSize);
			
			if (publishers.size() > 0) {
				globalMap.put("list", publishers);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			}
			
			
		} 
		else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("insertAPublisher/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertAPublisher(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("publisherISBN") String publisherISBN, @FormParam("publisherPlace") String publisherPlace, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			Publisher publisher = new Publisher(name, "", publisherISBN, publisherPlace);
			
			int flag = publisherImpl.insertOne(publisher);
			if (flag == 1) {
				globalMap.put(STATUS_CODE, SUCCESS);
			} else {
				globalMap.put(STATUS_CODE, INSERT_WR);
			}
		} else {

		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAPublisher/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateAPublisher(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("publisherISBN") String publisherISBN, @FormParam("publisherPlace") String publisherPlace, 
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Publisher> publishers = publisherImpl.selectById(id);
			if (publishers.size() > 0) {
				Publisher publisher = publishers.get(0);
				publisher.setInputName("");
				publisher.setName(name);
				publisher.setPublisherISBN(publisherISBN);
				publisher.setPublisherPlace(publisherPlace);
				
				int flag = publisherImpl.updateOne(publisher);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, SELECT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else if (publishers.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("deleteAPublisher/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteAPublisher(@PathParam("idadmin") String idadmin,  
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int flag = publisherImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectByCho/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCho(@PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @FormParam("cho") String cho) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
			
		int count = publisherImpl.countCho(cho);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Publisher> publishers = publisherImpl.selectByCho(cho, page.getBeginIndex(), pageSize);
			
			if (publishers.size() > 0) {
				globalMap.put("list", publishers);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			}
			
			
		} 
		else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
}
